<?php

class OrderModel extends Model
{
	static public function getInstance()
	{
		static $obj = null;
		if($obj == null){
			$obj = M('Orders');
		}
		return $obj;
	}

	static public function finishOrders($order_ids)
	{
		$model = self::getInstance();
		$logmodel = M('Order_log');
		foreach($order_ids as $id){
			$map['order_id'] = $id;

			$order = $model->where($map)->find();
			if($order['status'] == 'dead' || $order['status'] == 'finish'){
				continue;
			}
			$data['status'] = 'finish';
			$res = $model->where($map)->save($data);

			if($res){
				$log['order_id'] = $id;
				$log['op_name'] = session('admin_name');
				$log['log_text'] = "订单：{$id}完成";
				$log['behavior'] = '订单完成';
				$log['acttime'] = time();
				$log['result'] = 'success';
				$logmodel->add($log);
			}
		}
	}

	static public function deadOrders($order_ids)
	{
		$model = self::getInstance();
		$logmodel = M('Order_log');
		foreach($order_ids as $id){
			$map['order_id'] = $id;

			$order = $model->where($map)->find();
			if($order['payed'] > 0 || $order['status'] == 'dead' || $order['status'] == 'finish'){
				continue;
			}
			$data['status'] = 'dead';
			$res = $model->where($map)->save($data);

			if($res){
				$log['order_id'] = $id;
				$log['op_name'] = session('admin_name');
				$log['log_text'] = "订单：{$id}作废";
				$log['behavior'] = '订单作废';
				$log['acttime'] = time();
				$log['result'] = 'success';
				$logmodel->add($log);
			}
		}
	}
}